package com.scpii.universal.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.UserInfoDto;

public class LoginFilter implements Filter {

	private String onErrorUrl;
	private String excludeUrl;

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		HttpSession session = req.getSession();
		UserInfoDto userInfoDto = (UserInfoDto) session
				.getAttribute(GenericConstants.USER_INFO);

		String contextPath = req.getContextPath();
		String url = req.getRequestURI().replace(contextPath, "");
		if (url.equals("")) {
			url += "/";
		}
		if (userInfoDto != null
				&& ("/login.jsp".equals(url) || "/".equals(url))) {
			if (userInfoDto.getType() == 0) {
				res.sendRedirect(contextPath + "/produce/index.html");
			} else {
				res.sendRedirect(contextPath + "/login/loginUser.action");
			}
			return;
		}
		if (url.startsWith("//login") || url.startsWith("/login")
				|| url.startsWith("/dataBinding/goContentDetail")
				|| url.startsWith("/manage") || url.startsWith(excludeUrl)
				|| url.startsWith("/sd.jsp")
				|| url.startsWith("/produce/getLastAndroidDownUrl")
				|| url.startsWith("/ad.jsp")
				|| url.startsWith("/produce/getLastIosDownUrl")
				|| url.startsWith("/api") || url.startsWith("/favicon.ico")
				|| url.startsWith("/images") || url.startsWith("/css")
				|| url.startsWith("/rpc") || url.startsWith("/adl")) {// 若访问后台资源

			chain.doFilter(req, res);
			// 过滤到login
		} else if (userInfoDto == null) {// 转入管理员登陆页面
			// res.sendRedirect(contextPath + "/login.jsp");
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<script type=\"text/javascript\" src=\"" + contextPath
					+ "/manage/js/jquery-1.4.4.min.js\"></script>");
			out.println("<script type=\"text/javascript\">");
			out.println("window.open ('" + contextPath
					+ "/login.jsp?jumptologin','_top')");
			out.println("</script>");
			out.println("</html>");
			return;

		} else {
			chain.doFilter(req, res);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

		/*
		 * Returns a String containing the value of the named initialization
		 * parameter, or null if the parameter does not exist.
		 */
		onErrorUrl = filterConfig.getInitParameter("onError");
		excludeUrl = filterConfig.getInitParameter("excludeUrl");
		if (onErrorUrl == null || "".equals(onErrorUrl)) {
			onErrorUrl = "/login/quitLogin.action";
		}
	}

}
